package com.hyt.it.ogt.kq.service.gov.schedule.impl.support;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.hyt.it.ogt.kq.service.gov.schedule.impl.ExamRoomScheduleImpl.PackageExamRoomCandidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.utils.SupportUtils;

/***
 * 编排方式为多科目并不需要保持考生考场的组合考生方式
 */
public class CandidateMulAndNoMainten implements CandidateTogether {
    /***
     * 组合考生
     */
    public void togetherCandidate(Map<String, Map<String, PackageExamRoomCandidate>> packageExamRoomCandidate) {
        Map<String, Map<String, List<List<Candidate>>>> groupBatchCandidates = SupportUtils
                .transform(packageExamRoomCandidate);
        for (Map.Entry<String, Map<String, List<List<Candidate>>>> entry : groupBatchCandidates.entrySet()) {
            Map<String, List<List<Candidate>>> batchCandidateMap = entry.getValue();
            // 将分组内按批次考生数量对批次考生进行排序。 处理结果 ： <批次号: 考生列表>
            Map<String, List<Candidate>> sortedBatchCandidateMap = SupportUtils
                    .sortByCandidateNumber(SupportUtils.flatMap(batchCandidateMap));
            this.together(sortedBatchCandidateMap);
        }
    }

    /***
     * 组合虚拟考生
     * 采轮换的方式， 从每批次考生挑选一名考生，组合成虚拟考生
     * 
     * @param sortedBatchCandidateMap 分组内所有批次的考生
     */
    private void together(Map<String, List<Candidate>> sortedBatchCandidateMap) {
        List<String> batchNoList = new ArrayList<>(sortedBatchCandidateMap.keySet());
        List<List<Candidate>> batchCandidateList = new ArrayList<>(sortedBatchCandidateMap.values());
        for (List<Candidate> candidateList : batchCandidateList) {
            Collections.shuffle(candidateList);
        }

        int maxCandidateNumber = batchCandidateList.get(0).size();
        for (int index = 0; index < maxCandidateNumber; index++) {
            List<Candidate> togetherList = new ArrayList<>();
            for (int batchIndex = 0; batchIndex < batchNoList.size(); batchIndex++) {
                if (index < batchCandidateList.get(batchIndex).size()) {
                    togetherList.add(batchCandidateList.get(batchIndex).get(index));
                }
            }
            this.createCandidateVirId(batchNoList, togetherList);
        }
    }

}
